package com.yuerlive.paperplane.data.source.repository;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.yuerlive.paperplane.data.ZhihuDailyNewsQuestion;
import com.yuerlive.paperplane.data.source.datasource.ZhihuDailyNewsDataSource;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Admin on 2017/10/12.
 */

public class ZhihuDailyNewsRepository implements ZhihuDailyNewsDataSource{
    @Nullable
    private static ZhihuDailyNewsRepository instance=null;

    @NonNull
    private  ZhihuDailyNewsDataSource mLocalDataSource;

    @NonNull
    private  ZhihuDailyNewsDataSource mRemoteDataSoure;

    private Map<Integer,ZhihuDailyNewsQuestion> mCacheItems;

    @Override
    public void getZhihuDailyNews(boolean forceUpdate, boolean clearCache, long date, @NonNull loadZhihuDailyNewsCallback callback) {
        if(mCacheItems!=null && !forceUpdate){
           callback.onNewsLoad(new ArrayList<>(mCacheItems.values()));
            return;
        }
        mRemoteDataSoure.getZhihuDailyNews(false, clearCache, date, new loadZhihuDailyNewsCallback() {
            @Override
            public void onNewsLoad(@NonNull List<ZhihuDailyNewsQuestion> list) {
                refreshCache(clearCache,list);
                callback.onNewsLoad(new ArrayList<>(mCacheItems.values()));
                saveAll(list);
            }

            @Override
            public void onDataNotAvailable() {
              mLocalDataSource.getZhihuDailyNews(false, false, date, new loadZhihuDailyNewsCallback() {
                  @Override
                  public void onNewsLoad(@NonNull List<ZhihuDailyNewsQuestion> list) {
                      refreshCache(clearCache,list);
                      callback.onNewsLoad(new ArrayList<>(mCacheItems.values()));
                  }

                  @Override
                  public void onDataNotAvailable() {
                    callback.onDataNotAvailable();
                  }
              });
            }
        });
    }
    private ZhihuDailyNewsRepository(@NonNull ZhihuDailyNewsDataSource remoteDataSource,
                                     @NonNull ZhihuDailyNewsDataSource localDataSource){
      this.mLocalDataSource=localDataSource;
        this.mRemoteDataSoure=remoteDataSource;
    }
    public static ZhihuDailyNewsRepository getInstance(@NonNull ZhihuDailyNewsDataSource remoteDataSource,
                                                       @NonNull ZhihuDailyNewsDataSource localDataSource){
      if(instance==null){
          instance =new ZhihuDailyNewsRepository(remoteDataSource, localDataSource);
      }
      return  instance;
    }
    public static void destoryInstance(){
        instance=null;
    }
    @Override
    public void getFavorites(@NonNull loadZhihuDailyNewsCallback callback) {

    }

    @Override
    public void getItem(int itemId, @NonNull GetNewsItemCallback callback) {

    }

    @Override
    public void favoriteItem(int itemId, boolean favorite) {

    }

    @Override
    public void saveAll(@NonNull List<ZhihuDailyNewsQuestion> list) {
  //   mLocalDataSource.saveAll(list);
        mRemoteDataSoure.saveAll(list);
        if(mCacheItems==null){
            mCacheItems =new LinkedHashMap<>();
        }
        for(ZhihuDailyNewsQuestion item:list){
            mCacheItems.put(item.getId(),item);
        }
    }
    private void refreshCache(boolean clearCache,List<ZhihuDailyNewsQuestion> list){
        if(mCacheItems==null){
            mCacheItems=new LinkedHashMap<>();
        }
        if(clearCache){
            mCacheItems.clear();
        }
        for(ZhihuDailyNewsQuestion item:list){
            mCacheItems.put(item.getId(),item);
        }
    }
}
